﻿using LightPersist.Exceptions;

namespace LightPersist.Transactions
{
	internal enum TransactionState
	{
		Unknown = 0,
		Started,
		InnerCommitPostponed,
		Processing,
		Commited,
		RolledBack,
		Error,
	}

	internal static class TransactionStateExt
	{
		public static bool CanRollback(this TransactionState state)
		{
			switch (state)
			{
				case TransactionState.Commited:
				case TransactionState.InnerCommitPostponed:
				case TransactionState.RolledBack:
					return false;
				case TransactionState.Started:
				case TransactionState.Error:
					return true;
				default:
					throw new LightPersistException("Unknown behavior for TransactionState: " + state);
			}
		}

		public static bool CanCommit(this TransactionState state)
		{
			switch (state)
			{
				case TransactionState.InnerCommitPostponed:
				case TransactionState.Started:
					return true;
				case TransactionState.Commited:
				case TransactionState.RolledBack:
				case TransactionState.Error:
					return false;
				default:
					throw new LightPersistException("Unknown behavior for TransactionState: " + state);
			}
		}

		public static bool CanAddNested(this TransactionState state)
		{
			switch (state)
			{
				case TransactionState.InnerCommitPostponed:
				case TransactionState.Commited:
				case TransactionState.RolledBack:
				case TransactionState.Error:
					return false;
				case TransactionState.Started:
					return true;
				default:
					throw new LightPersistException("Unknown behavior for TransactionState: " + state);
			}
		}
	}
}